"""
 -*- coding: utf-8-*-
 @File: mgt_exception.py
 @Author: chen
 @time: 2023/10/04/ 17:30
"""
from hogwartsHomework.homework_app_auto_L3.utils.mgt_data_save import save_screenshot, save_page_source
from hogwartsHomework.homework_app_auto_L3.utils.mgt_log import logger


def record_exception(func):
    """
    异常处理装饰器
    装饰测试用例或方法，当被装饰代码出现异常会自动截图并打印page source
    """

    def _inner(*args, **kwargs):
        driver = args[0].driver
        try:
            return func(*args, **kwargs)
        except Exception as e:
            # driver = args[0].driver
            logger.error("出现异常！！！")
            logger.error(f"异常信息：{e}")
            save_screenshot(driver)
            save_page_source(driver)
            raise
    return _inner
